package com.soft.knowledge.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.knowledge.entity.Ebook;
import com.soft.knowledge.entity.EbookExample;
import com.soft.knowledge.mapper.EbookMapper;
import com.soft.knowledge.req.EbookByCategoryReq;
import com.soft.knowledge.req.EbookQueryReq;
import com.soft.knowledge.req.EbookSaveReq;
import com.soft.knowledge.resp.EbookQueryResp;
import com.soft.knowledge.resp.PageResp;
import com.soft.knowledge.utils.CopyUtil;
import com.soft.knowledge.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 电子书(Ebook)表服务实现类
 *
 * @author makejava
 * @since 2022-11-19 13:17:14
 */
@Service
public class EbookService{
    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);
    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;
    /**
     * EbookReq：请求参数列表
     * EbookResp: 返回参数列表
     * 最好不要出现Ebook类
     * @param req
     * @return
     */
    public PageResp<EbookQueryResp> list(EbookQueryReq req){

        EbookExample example = new EbookExample();
        EbookExample.Criteria criteria = example.createCriteria();
        //动态sql
        if (!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%"+req.getName()+"%");
        }
        //PageHelper和查询的list放到一起
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(example);
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总条数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
//        List<EbookResp> respList = new ArrayList<>();
//        for(Ebook ebook: ebookList){
////            EbookResp resp = new EbookResp();
////            BeanUtils.copyProperties(ebook, resp);
//            respList.add( CopyUtil.copy(ebook, EbookResp.class));
//        }

        PageResp<EbookQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(CopyUtil.copyList(ebookList, EbookQueryResp.class));
        return pageResp;
    }

    public void save(EbookSaveReq req){
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        if(ObjectUtils.isEmpty(ebook.getId())){
            //新增
            ebook.setId(snowFlake.nextId());
            ebookMapper.insert(ebook);
        }else{
            //更新
            ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    public void delete(Long id) {
        ebookMapper.deleteByPrimaryKey(id);
    }

    public List<EbookQueryResp> selectByCategory(EbookByCategoryReq req){
        EbookExample example = new EbookExample();
        EbookExample.Criteria criteria = example.createCriteria();
        criteria.andCategory1IdEqualTo(req.getCategory1Id())
                .andCategory2IdEqualTo(req.getCategory2Id());
        List<Ebook> ebookList = ebookMapper.selectByExample(example);

        return CopyUtil.copyList(ebookList, EbookQueryResp.class);
    }

}